﻿Imports DRO.connections.connections_admin
Imports DRO.DB.DB
Imports DRO.UTILITIES
Imports DRO.tokenizing_utilities
Imports DRO
Module support
    Dim ds As DataSet
    Dim num_recs As Integer
    Public defines As SortedList
    Public elapsed
    Public gm As GridManager
    Sub load_list(srtl As SortedList, table As String, a_key_field As String, Optional a_value_field As String = "")
        Dim sg As sql_generator
        sg = New sql_generator
        sg.verb = "SELECT DISTINCT"
        sg.add_list(table, a_key_field)
        If a_value_field > "" Then
            sg.add_list(table, "word_type,word_corrected,comment")
        End If
        Dim ds As DataSet
        Dim k As Integer
        ds = sg.dataset(k)
        If k < 0 Then
            Stop
        End If
        Dim i As Integer
        For i = 0 To k - 1
            Dim word As String
            word = get_field(ds, i, a_key_field).tolower
            If a_value_field > "" Then
                Dim an_entry As d_entry
                Dim comment As String
                Dim word_corrected As String
                Dim def As String
                an_entry = New d_entry
                def = get_field(ds, i, a_value_field)
                word_corrected = get_field(ds, i, "word_corrected")
                comment = get_field(ds, i, "comment")

                an_entry.word_type = def
                an_entry.comment = comment
                an_entry.word_corrected = word_corrected
                srtl.Add(word, an_entry)
            Else
                srtl.Add(word, word)
            End If
        Next
    End Sub
    '==========================================
    Sub set_up_cars()
        clear("cars")
        Dim sg As sql_generator
        sg = New sql_generator
        sg.verb = "SELECT"
        sg.add_list("clist")

        ds = get_dataset(sg, num_recs)
        If num_recs < 0 Then
            Stop
        End If
    End Sub
    Sub make_tokens(ds As DataSet)
        Dim delims As String = " ()"
        Dim i As Integer
        Dim inner_html As String
        num_recs = ds.Tables(0).Rows.Count
        For i = 0 To num_recs - 1
            'Dim dr As DataRow
            inner_html = nz(get_field(ds, i, "inner_html")).tolower

            inner_html = inner_html.Replace("*", "")
            inner_html = inner_html.Replace("$$$$$$$$$$$$$$$", "")
            inner_html = inner_html.Replace("$$$$$$$$$$$$$$", "")
            Debug.Print("make_tokens: " & inner_html)
            'Stop
            'Stop
            tokens = get_tokens_all(inner_html, delims)
            classify_tokens()


            'Stop
        Next
    End Sub
    Dim delims As String = " ()"
    Sub process_tokens(inner_html As String)
        inner_html = inner_html.ToLower

        inner_html = inner_html.Replace("*", "")
        inner_html = inner_html.Replace("$$$$$$$$$$$$$$$", "")
        inner_html = inner_html.Replace("$$$$$$$$$$$$$$", "")
        Debug.Print("make_tokens: " & inner_html)
        'Stop
        'Stop
        'inner_html = "4x4"
        tokens = get_tokens_all_REAL(inner_html, delims)
        classify_thru_dictionary()


    End Sub
    '===============================================
    Dim location As String
    Dim make As String
    Dim model As String
    Dim model_year As String
    Dim price As String
    Dim tokens() As String
    Sub classify_tokens()
        Dim t As String
        make = "unknown"
        model = "unknown"
        model_year = "unknown"
        price = "9999"
        Dim si As sql_generator
        si = New sql_generator
        si.verb = "INSERT"
        si.add_list("cars")
        Dim years As String
        years = "-1995-1996-1997-1998-1999-2000-2001-2002-2003-2004-2005-2008-2009-2010-2011-"
        years = years & "95-96-97-98-99-"

        For Each t In tokens
            Debug.Print("classify: " & t)
            If t Is Nothing Then
            Else
                If t = "[" Then GoTo next_one
                If t.StartsWith("$") And price = "9999" Then
                    price = t.Replace(",", "")

                    si.add_insert_value("price", price)
                End If
                If years Like "*-" & t & "-*" And model_year = "unknown" Then
                    model_year = t
                    si.add_insert_value("model_year", model_year)
                End If
                If t = "toyota" Then
                    make = t
                    si.add_insert_value("make", make)
                End If
                If "-camry-corolla-tercel-tundra-" Like "*-" & t & "-*" And model = "unknown" Then
                    model = t
                    si.add_insert_value("model", model)
                End If
            End If
            'Stop
next_one:
        Next
        Dim k As Integer
        si.show_statement()
        k = si.update_database
        If k < 0 Then
            Stop
        End If

    End Sub
    Dim year As String
    Public sg As sql_generator
    Sub classify_thru_dictionary()
        Dim i As Integer
        Dim ntok As Integer
        Dim word_type As String
        clear("entry_words")
        ntok = UBound(tokens)
        i = 0
        Do
            i = i + 1
            If i > ntok Then Exit Do
            Dim t As String
            t = tokens(i)
            word_type = ""
            Debug.Print("classify: " & i & " " & t)
            Dim an_e As d_entry

            an_e = defines(t)
            If t.StartsWith("$") Then
                Dim c As String
                c = Mid(t, 2, 1)
                If "1" <= c And c <= "9" Then
                    'this is price
                    price = t
                    word_type = "price"
                    GoTo next_one
                End If
            End If
            If an_e Is Nothing Then
                'Stop
            Else
                If an_e.word_corrected > "" Then
                    t = an_e.word_corrected
                End If


                Select Case an_e.word_type
                    Case "read till closing )"
                        'read till closing ")"
                        t = read_till_find(tokens, i, ")")
                        Debug.Print("classify: " & i & " " & t)
                        location = t
                    Case "model"
                        model = t
                    Case "make"
                        make = t
                    Case "year"
                        year = t
                End Select
            End If

next_one:

            sg = New sql_generator
            sg.verb = "INSERT"
            sg.add_list("entry_words")
            sg.add_insert_value("word", t)
            sg.add_insert_value("word_type", word_type)
            Dim k As Integer
            Dim err1 As New Exception("empty")
            k = sg.update_database(err1)
            If k < 0 Then
                Stop
            End If
        Loop While i <= ntok
    End Sub
    Function read_till_find(tokens() As String, ByRef ind As Integer, final_char As String)
        Dim ntok As Integer
        Dim res As String
        res = ""
        ntok = UBound(tokens)
        ind += 1
        Do While ind <= ntok
            Dim t As String
            t = tokens(ind)
            If t = ")" Then
                Exit Do
            End If
            res = res + " " + t
            ind += 1
        Loop
        res = res.Trim
        Return res
    End Function
    Public Class d_entry
        Public word As String
        Public word_corrected As String
        Public word_type As String
        Public comment As String
    End Class
    Public stp As Stopwatch

End Module
